#!/bin/bash

DC_HOME=/data/xwaf_admin
DC_BIN=$DC_HOME/xwaf_admin
DC_LOG=$DC_HOME/log.run
DC_PID=$DC_HOME/run.pid
DC_NAME="xwaf_admin"

. /etc/rc.d/init.d/functions

start() {
    echo "`date +%Y/%m/%d\ %H:%M:%S` INFO:starting xwaf_admin" >>$DC_LOG
    #netstat -nplt|awk '$4 ~ /\<'8001'\>/'|grep "xwaf_admin"
    ps -ef|grep "xwaf_admin"|egrep -v "grep|$0" >/dev/null 
    if [ $? -eq 0 ];then
      echo "xwaf_admin allready running!" 
    else
       rm -f $DC_PID
       cd $DC_HOME
       $DC_BIN >>$DC_LOG 2>&1 &
       echo $! >$DC_PID
       sleep 1
       ps -ef|grep "xwaf_admin"|grep -v grep >/dev/null
       if [ $? -eq 0 ]; then
           action $"Starting $DC_NAME: " /bin/true
       else
           action $"Starting $DC_NAME: " /bin/false
           rm -f $DC_PID
       fi
    fi

}

stop() {
    echo "`date +%Y/%m/%d\ %H:%M:%S` INFO:stoping xwaf_admin" >>$DC_LOG
    kill `cat $DC_PID`
    if [ $? -eq 0 ]; then
        rm -f $DC_PID
        action $"Stopping $DC_NAME: " /bin/true
    else
        action $"Stopping $DC_NAME: " /bin/false
    fi

}

restart () {
    stop
    sleep 1
    start
}


case "$1" in
   start)
     start;;
   stop)
     stop;;
   restart)
     restart;;
   *)
    echo "start|stop|restart"
    exit 1;;
esac
